package TetrisFlash.InGameManager 
{
	import starling.display.Image;
	import starling.display.QuadBatch;
	import starling.display.Sprite;
	import starling.textures.Texture;
	import TetrisFlash.GameLoadData.AssetsTexture;
	import TetrisFlash.Objects.TetrisBoard.TetrisBoard;
	import TetrisFlash.Objects.Tetrominoes.ITetromino;
	import TetrisFlash.Screen.Screen;
	import TetrisFlash.GlobalData.Global;
	import starling.utils.deg2rad;
	/**
	 * ...
	 * @author DANTRUONG
	 */
	public class ManagerDrawGame 
	{
		//quadbatch to draw board
		public var boardQuadBatch:QuadBatch;
		
		//image element of board
		public var boardImage:Image;
		
		//image element of tetromino
		public var tetroImage:Image;
		
		//quadbatch to draw tetro next
		public var tetroNextQuadBatch:QuadBatch;
		
		//quadbatch to draw tetro hold
		public var tetroHoldQuadBatch:QuadBatch;
		
		//time start Effect
		public var timeStartEffect:Number;
		
		public var isEffect:Boolean;
		public var isDrawed:Boolean;
		
		//Sprite effect clear
		public var spriteClear: Sprite;
		public var rotation: int;
		
		//Screen to draw this game
		public var screen:Screen;
		
		//list image element of tetro
		public var boardImage1:Image;
		public var boardImage2:Image;
		public var boardImage3:Image;
		public var boardImage4:Image;
		public var boardImage5:Image;
		public var boardImage6:Image;
		public var boardImage7:Image;
		
		public function ManagerDrawGame(_screen: Screen) 
		{
			screen = _screen;
			isEffect = false;
			isDrawed = false;
			timeStartEffect = 0;
			
			//create quadbatch for board
			boardQuadBatch = new QuadBatch();
			
			//create sprite effect clear
			spriteClear = new Sprite();
			rotation = 0;
			
			//create quadbatch for tetro next
			tetroNextQuadBatch = new QuadBatch();
			
			//create quadbatch for tetro hold
			tetroHoldQuadBatch = new QuadBatch();
			
			//create list image element
			boardImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_0"));
			boardImage1 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_1"));
			boardImage2 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_2"));
			boardImage3 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_3"));
			boardImage4 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_4"));
			boardImage5 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_5"));
			boardImage6 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_6"));
			boardImage7 = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_7"));
		}
		
		public function update():void {
			if (isEffect) {
				screen.addChild(spriteClear);
				for (var i:int = 0; i < spriteClear.numChildren; i++) {
					for (var j:int = 0; j < (spriteClear.getChildAt(i) as Sprite).numChildren; j++) {
						(spriteClear.getChildAt(i) as Sprite).getChildAt(j).rotation = deg2rad(rotation);
					}
				}
				timeStartEffect += screen.myGame.gameDeltaTime;
				rotation += 8;
				
				if (timeStartEffect >= Global.TIME_CLEAR) {
					timeStartEffect = 0;
					isEffect = false;
					
					rotation = 0;
					if (screen.contains(spriteClear)) {
						screen.removeChild(spriteClear);
						spriteClear.dispose();
						spriteClear = new Sprite();
						isDrawed = false;
					}
				}
			}
		}
		
		public function drawTetromino(tetro: ITetromino): void {
			tetro.updateDrawTetromino();
		}
		
		public function drawTetrisBoard(tetrisBoard: TetrisBoard):void {
			
			/*Solution 1
			screen.addChild(boardQuadBatch);*/

			if (spriteClear.numChildren > 0) {
				isDrawed = true;
			}
			
			for (var j:int = 2; j < tetrisBoard.row; j++) {
				if (!tetrisBoard.isFullLine(j)) {
					for (var i: int = 0; i < tetrisBoard.column; i++) {
						if (tetrisBoard.boardArray[i][j]) {
							boardImage.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
							boardImage.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
							boardImage.name = "c" + i + "r" + j;
							boardQuadBatch.addImage(boardImage);
							boardImage.dispose();
						}
					}
				}else {
					//add elements of sprite effect
					if (!isDrawed) {
						spriteClear.addChild(createSpriteClear(tetrisBoard, j));
					}
				}
			}
		}
		
		public function updateDrawBoard(tetrisBoard: TetrisBoard):void {
			/*Solution 1
			 * if(screen.contains(boardQuadBatch)){
				screen.removeChild(boardQuadBatch);
			}
			boardQuadBatch.reset();*/
			
			//if screen not contain quadbatch then add quadbatch
			if(!screen.contains(boardQuadBatch)){
				screen.addChild(boardQuadBatch);
			}
			
			//reset all data of board but not dispose
			boardQuadBatch.reset();
			
			//draw board
			drawTetrisBoard(tetrisBoard);
		}
		
		
		public function drawTetrisBoardWithColor(tetrisBoard: TetrisBoard):void {
			/* Solution 1
			 * screen.addChild(boardQuadBatch);*/

			if (spriteClear.numChildren > 0) {
				isDrawed = true;
			}
			
			for (var j:int = 2; j < tetrisBoard.row; j++) {
				if (!tetrisBoard.isFullLine(j)) {
					for (var i: int = 0; i < tetrisBoard.column; i++) {
						if (tetrisBoard.boardArray[i][j]) {
							switch(tetrisBoard.boardArray[i][j]) {
								case 1:
									boardImage1.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage1.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage1.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage1);
									boardImage1.dispose();
									break;
								case 2:
									boardImage2.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage2.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage2.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage2);
									boardImage2.dispose();
									break;
								case 3:
									boardImage3.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage3.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage3.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage3);
									boardImage3.dispose();
									break;
								case 4:
									boardImage4.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage4.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage4.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage4);
									boardImage4.dispose();
									break;
								case 5:
									boardImage5.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage5.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage5.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage5);
									boardImage5.dispose();
									break;
								case 6:
									boardImage6.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage6.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage6.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage6);
									boardImage6.dispose();
									break;
								case 7:
									boardImage7.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
									boardImage7.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
									boardImage7.name = "c" + i + "r" + j;
									boardQuadBatch.addImage(boardImage7);
									boardImage7.dispose();
									break;
							}
							
						}
					}
				}else {
					//add elements of sprite effect
					if (!isDrawed) {
						spriteClear.addChild(createSpriteClear(tetrisBoard, j));
					}
				}
			}
		}
		
		public function updateDrawBoardWithColor(tetrisBoard: TetrisBoard):void {
			/* Solution 1
			 * if(screen.contains(boardQuadBatch)){
				screen.removeChild(boardQuadBatch);
			}
			boardQuadBatch.reset();*/
			
			//if screen not contain quadbatch then add quadbatch
			if(!screen.contains(boardQuadBatch)){
				screen.addChild(boardQuadBatch);
			}
			
			//reset all data of board but not dispose
			boardQuadBatch.reset();
			
			//draw board with color
			drawTetrisBoardWithColor(tetrisBoard);
		}
		
		public function createSpriteClear(tetrisBoard: TetrisBoard, j:int):Sprite {
			var spriteClearRow:Sprite = new Sprite();
			var textureBrick0:Texture = AssetsTexture.getTetrisAtlas().getTexture("Brick_0");
			for (var k: int = 0; k < tetrisBoard.column; k++) {
				var image:Image = new Image(textureBrick0);
				image.pivotX = image.width / 2;
				image.pivotY = image.height / 2;
				image.x = Global.OFFSET_TOBOARD_X + k * Global.CELL_WIDTH + image.pivotX;
				image.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH + image.pivotY;
				spriteClearRow.addChild(image);
			}
			return spriteClearRow;
		}
		
		public function drawTetrominoNextXY(ID:int , X:int, Y:int):void {
			if (ID == 0) {
				return;
			}
			
			var tetrominoArray:Array;
			var column:int, row:int;
			var i:int = 0;
			switch(ID) {
				case Global.ID_TETROMINO_I:
					column = 4;
					row = 1;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] =
					tetrominoArray[3][0] = ID;
					break;
				case Global.ID_TETROMINO_L1:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[0][1] = ID;
					tetrominoArray[1][1] = tetrominoArray[2][1] = 0;
					break;
				case Global.ID_TETROMINO_L2:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[2][1] = ID;
					tetrominoArray[1][1] = tetrominoArray[0][1] = 0;
					break;
				case Global.ID_TETROMINO_O:
					column = 2;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[0][1] = tetrominoArray[1][0] = 
					tetrominoArray[1][1] = ID;
					break;
				case Global.ID_TETROMINO_T:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][1] = tetrominoArray[2][1] = 0;
					break;
				case Global.ID_TETROMINO_Z1:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][1] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][1] = tetrominoArray[2][0] = 0;
					break;
				case Global.ID_TETROMINO_Z2:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[2][0] = tetrominoArray[1][0] = tetrominoArray[0][1] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][0] = tetrominoArray[2][1] = 0;
					break;
			}
			
			tetroImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_" + ID));
			
			/* Solution 1
			 * if(screen.contains(tetroNextQuadBatch)){
				screen.removeChild(tetroNextQuadBatch);
			}
			tetroNextQuadBatch.dispose();
			tetroNextQuadBatch = new QuadBatch();*/
			
			//if screen not contain tetro next quadbatch then add this
			if (!screen.contains(tetroNextQuadBatch)) {
				screen.addChild(tetroNextQuadBatch);
			}
			
			//reset tetro next quadbatch data
			tetroNextQuadBatch.reset();
			
			//draw tetro next quadbatch
			screen.addChild(tetroNextQuadBatch);
			
			for (i = 0; i < column; i++) {
				for (var j:int = 0; j < row; j++) {
					if (tetrominoArray[i][j]) {
						tetroImage.x = X + (4 - column) * Global.CELL_WIDTH / 2 + i * Global.CELL_WIDTH;
						tetroImage.y = Y + (2 - row) * Global.CELL_WIDTH / 2 + j * Global.CELL_WIDTH;
						tetroNextQuadBatch.addImage(tetroImage);
						tetroImage.dispose();
					}
				}
			}
		}
		
		public function drawTetrominoHoldXY(ID:int, X:int, Y:int):void {
			if (ID == 0) {
				return;
			}
			
			var tetrominoArray:Array;
			var column:int, row:int;
			var i:int = 0;
			switch(ID) {
				case Global.ID_TETROMINO_I:
					column = 4;
					row = 1;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] =
					tetrominoArray[3][0] = ID;
					break;
				case Global.ID_TETROMINO_L1:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[0][1] = ID;
					tetrominoArray[1][1] = tetrominoArray[2][1] = 0;
					break;
				case Global.ID_TETROMINO_L2:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[2][1] = ID;
					tetrominoArray[1][1] = tetrominoArray[0][1] = 0;
					break;
				case Global.ID_TETROMINO_O:
					column = 2;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[0][1] = tetrominoArray[1][0] = 
					tetrominoArray[1][1] = ID;
					break;
				case Global.ID_TETROMINO_T:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][0] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][1] = tetrominoArray[2][1] = 0;
					break;
				case Global.ID_TETROMINO_Z1:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[0][0] = tetrominoArray[1][0] = tetrominoArray[2][1] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][1] = tetrominoArray[2][0] = 0;
					break;
				case Global.ID_TETROMINO_Z2:
					column = 3;
					row = 2;
					tetrominoArray = new Array();
					for (i = 0; i < column; i++)
					{
						tetrominoArray[i] = new Array();
					}
					tetrominoArray[2][0] = tetrominoArray[1][0] = tetrominoArray[0][1] = 
					tetrominoArray[1][1] = ID;
					tetrominoArray[0][0] = tetrominoArray[2][1] = 0;
					break;
			}
			
			tetroImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_" + ID));
			
			/* Solution 1
			 * if(screen.contains(tetroHoldQuadBatch)){
				screen.removeChild(tetroHoldQuadBatch);
			}
			tetroHoldQuadBatch.dispose();
			tetroHoldQuadBatch = new QuadBatch();*/
			
			//if screen not contain tetro hold quadbatch then add this
			if (!screen.contains(tetroHoldQuadBatch)) {
				screen.addChild(tetroHoldQuadBatch);
			}
			
			//reset tetro hold quadbatch data
			tetroHoldQuadBatch.reset();
			
			//draw tetro hold quadbatch
			screen.addChild(tetroHoldQuadBatch);
			
			for (i = 0; i < column; i++) {
				for (var j:int = 0; j < row; j++) {
					if (tetrominoArray[i][j]) {
						tetroImage.x = X + (4 - column) * Global.CELL_WIDTH / 2 + i * Global.CELL_WIDTH;
						tetroImage.y = Y + (2 - row) * Global.CELL_WIDTH / 2 + j * Global.CELL_WIDTH;
						tetroHoldQuadBatch.addImage(tetroImage);
						tetroImage.dispose();
					}
				}
			}
		}
		
		public function ReleaseManagerDrawGame():void {
			if(screen.contains(tetroHoldQuadBatch)){
				screen.removeChild(tetroHoldQuadBatch);
			}
			
			if(screen.contains(tetroNextQuadBatch)){
				screen.removeChild(tetroNextQuadBatch);
			}
			
			if(screen.contains(boardQuadBatch)){
				screen.removeChild(boardQuadBatch);
			}
			
			if (screen.contains(spriteClear)){
				screen.removeChild(spriteClear);
			}
				
			tetroHoldQuadBatch.dispose();
			tetroNextQuadBatch.dispose();
			boardQuadBatch.dispose();
			spriteClear.dispose();
			
			tetroImage.dispose();
			boardImage1.dispose();
			boardImage2.dispose();
			boardImage3.dispose();
			boardImage4.dispose();
			boardImage5.dispose();
			boardImage6.dispose();
			boardImage7.dispose();
		}
		
	}
	
}